package com.iraeta.controller.gift;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.gift.Gift;
import com.iraeta.entity.resulttype.GiftResultType;
import com.iraeta.service.gift.IGiftService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController(value = "GiftController")
@RequestMapping("gift/gift")
public class GiftController extends BaseController {
    @Resource
    IGiftService iGiftService;

    @RequestMapping("listgift")
    public String listGift(@RequestBody JSONObject jo){
        int typeId = jo.getIntValue("typeId");
        String searchName = jo.getString("searchName");
        this.pageSet(jo);
        if(typeId == 0){
            List<GiftResultType> giftList = iGiftService.listGiftByName(searchName);
            PageInfo<GiftResultType> giftPageInfo = this.getPageInfo(giftList);
            return this.success(giftPageInfo);
        }else{
            List<GiftResultType> giftList = iGiftService.listGiftByTypeId(typeId, searchName);
            PageInfo<GiftResultType> giftPageInfo = this.getPageInfo(giftList);
            return this.success(giftPageInfo);
        }
    }

    @RequestMapping("addeidtgift")
    public String addEditGift(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Gift gift = id == 0 ? new Gift() :  iGiftService.getById(id);
        this.mapJsonToObj(paramJb, gift);
        if(id == 0){
            if(gift.getTypeId() == null) {
                gift.setTypeId(0);
            }
            iGiftService.save(gift);
        }else {
            iGiftService.updateById(gift);
        }
        return this.success(gift);
    }

    @RequestMapping("getgift")
    public String getGift(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        GiftResultType gift = iGiftService.getGift(id);
        return this.success(gift);
    }

    @RequestMapping("deletegift")
    public String deleteGift(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        iGiftService.deleteGift(id);
        return this.success();
    }

    @RequestMapping("tongbugiftmodel")
    public String tongBuGiftModel(@RequestBody JSONObject paramJb){
        int sourcegiftid = paramJb.getIntValue("sourcegiftid");
        int targetgiftid = paramJb.getIntValue("targetgiftid");
        iGiftService.tongBuGiftModel(sourcegiftid, targetgiftid);
        return this.success();
    }
}
